package it.stasbranger.myproject.client.managed.activity;

import com.google.gwt.activity.shared.Activity;

import com.google.gwt.event.shared.EventBus;

import com.google.gwt.place.shared.Place;

import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.Receiver;

import com.google.gwt.user.client.ui.AcceptsOneWidget;

import it.stasbranger.myproject.client.managed.request.ApplicationRequestFactory;
import it.stasbranger.myproject.client.managed.request.RequestsProxy;
import it.stasbranger.myproject.client.managed.request.SuppliersHasRequestsProxy;

import it.stasbranger.myproject.client.managed.ui.SuppliersHasRequestsProxyRenderer;

import it.stasbranger.myproject.client.scaffold.activity.IsScaffoldMobileActivity;

import it.stasbranger.myproject.client.scaffold.place.ProxyEditView;
import it.stasbranger.myproject.client.scaffold.place.ProxyListPlace;
import it.stasbranger.myproject.client.scaffold.place.ProxyPlace;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class RequestsEditActivityWrapper implements Activity,
		IsScaffoldMobileActivity {

	/**
	 * The view used by this activity.
	 *
	 */
	public interface View<V extends ProxyEditView<RequestsProxy, V>> extends
			ProxyEditView<RequestsProxy, V> {

		void setSuppliersHasRequestsesPickerValues(Collection<SuppliersHasRequestsProxy> values);
	}

	private final EntityProxyId<RequestsProxy> proxyId;
	private final ApplicationRequestFactory requests;
	private final View<?> view;
	private final Activity wrapped;

	public RequestsEditActivityWrapper(ApplicationRequestFactory requests, View<?> view, Activity activity, EntityProxyId<RequestsProxy> proxyId) {
		this.requests = requests;
		this.view = view;
		this.wrapped = activity;
		this.proxyId = proxyId;
	}

	public Place getBackButtonPlace() {
		return (proxyId == null) ? new ProxyListPlace(RequestsProxy.class) : new ProxyPlace(proxyId, ProxyPlace.Operation.DETAILS);
	}

	public String getBackButtonText() {
		return "Cancel";
	}

	public Place getEditButtonPlace() {
		return null;
	}

	public String getTitleText() {
		return (proxyId == null) ? "New " : "Edit ";
	}

	public boolean hasEditButton() {
		return false;
	}

	public String mayStop() {
		return wrapped.mayStop();
	}

	public void onCancel() {
		wrapped.onCancel();
	}

	public void onStop() {
		wrapped.onStop();
	}

	public void start(AcceptsOneWidget display, EventBus eventBus) {

		view.setSuppliersHasRequestsesPickerValues(Collections.<SuppliersHasRequestsProxy> emptyList());
		requests.suppliersHasRequestsRequest().findAllSuppliersHasRequestss(0, 50).with(SuppliersHasRequestsProxyRenderer.instance().getPaths()).fire(new Receiver<List<SuppliersHasRequestsProxy>>() {
			public void onSuccess(List<SuppliersHasRequestsProxy> response) {
				List<SuppliersHasRequestsProxy> values = new ArrayList<SuppliersHasRequestsProxy>();
				values.add(null);
				values.addAll(response);
				view.setSuppliersHasRequestsesPickerValues(values);
			}
		});
		wrapped.start(display, eventBus);
	}
}
